package org.edu.modules.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.constant.ServiceNameConstants;
import org.edu.common.system.api.factory.SysBaseAPIFallbackFactory;
import org.edu.common.system.vo.LoginUser;
//import org.edu.modules.act.service.*;
import org.edu.modules.oa.entity.*;
import org.edu.modules.oa.service.*;
import org.edu.modules.oa.vo.VeOaMeetCheckTableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="OA公共数据")
@RestController
@RequestMapping("oa/veOaPublic")
@Slf4j
public class VeOaPublicController {

    @Resource
    private VeOaPublicService veOaPublicService;
    @Resource
    private IVeOaLaneMailService veOaLaneMailService;
    @Resource
    private IVeOaDiaryService iVeOaDiaryService;
    @Resource
    private IVeOaScheduleService iVeOaScheduleService;
    @Resource
    private IVeOaCheckMeetAnnounceService iVeOaCheckMeetAnnounceService;
    @Resource
    private IVeOaMeetCheckService iVeOaMeetCheckService;
    @Resource
    private IVeOaMyJoinMeetService iVeOaMyJoinMeetService;
    @Resource
    private IVeOaMyApplyMeetService iVeOaMyApplyMeetService;
    @Resource
    private XoaCommonUserService xoaCommonUserService;
    //@Autowired
    //private HistoryService historyService;


    //1.	未读消息数量
    // 日志注解
    @AutoLog(value = "1.未读消息数量")
    @ApiOperation(value="获取未读消息", notes="根据用户id，获取未读、待办事项消息")
    @PostMapping(value = "/getNoReadMessage")
    public Result<?> getNoReadMessage(String userId, Integer startPage, Integer pageSize ){
        //HashMap<String, Object> map =  iVeOaMyApplyMeetService.showMyApplyMeet(startPage,pageSize,veOaMeetEntity);
        Map<String,Object> map= new HashMap<String,Object>();
        return Result.OK(map);
    }





    //3.	我的收件箱列表
    /**
     * 收件箱-动态分页查询
     * @param titleQuery 查询主题
     * @return 查询结果
     */
    @AutoLog(value = "3.我的收件箱列表")
    @ApiOperation(value="收件箱查询", notes="根据用户id获取收件箱列表信息")
    @PostMapping("/receiveMail")
    public Result<?> receiveMail(String titleQuery){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<VeOaLaneMailEntity> listTotal = veOaLaneMailService.list(
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",3)
                        .eq("toUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));
        /*IPage<VeOaLaneMailEntity> list  =  veOaLaneMailService.page(new Page<VeOaLaneMailEntity>(startPage,pageSize),
                new QueryWrapper<VeOaLaneMailEntity>()
                        .eq("is_deteted",0)
                        .eq("emailstate",3)
                        .eq("toUser",user.getId())
                        .like("emailTitle",titleQuery).orderByDesc("create_time"));*/
        Map<String,Object> map = new HashMap<>();
        //map.put("list",list);
        map.put("listTotal",listTotal);
        return Result.OK(map);
    }

    //4.	工作日程
    // 日志注解
    @AutoLog(value = "日程安排-我的日程安排")
    @ApiOperation(value="我的日程安排", notes="根据用户id获取用户的工作日程安排")
    // 显示表格
    @PostMapping(value = "/showRiChengTable")
    public Result<?> showTable(VeOaScheduleEntity VeOaScheduleEntity){
        HashMap<String, Object> map =  veOaPublicService.showMySchedule(VeOaScheduleEntity);
        return Result.OK(map);
    }


    //5.1.	审批的会议列表
    //一个提供审批的会议，一个提供参加的会议列表
    //应该根据不同用户的角色权限返回不同的列表。比如申请人，与审批人
    // 日志注解
    @AutoLog(value = "5.1.会议审核-显示表格")
    @ApiOperation(value="审批的会议", notes="提供审批的会议")
    // 显示表格
    @PostMapping(value = "/showMeetCheckTable")
    public Result<?> showTable(VeOaMeetCheckTableVo veOaMeetPlaceEntity) {
        HashMap<String, Object> map= veOaPublicService.showMeetCheckTable(veOaMeetPlaceEntity);

        return Result.OK(map);
    }

    //5.2.参加的会议列表
    // 日志注解
    @AutoLog(value = "5.2.我参加的会议-显示表格")
    @ApiOperation(value="参加的会议列表", notes="提供参加的会议列表")
    // 显示表格
    @PostMapping(value = "/showMyJoinMeetTable")
    public Result<?>showTable(VeOaMeetEntity veOaMeetEntity ){
        HashMap<String, Object> map= iVeOaMyJoinMeetService.ShowMyJoinMeet(null,null,veOaMeetEntity);
        return Result.OK(map);
    }


    //7.	我的通知列表
    // 日志注解
    @AutoLog(value = "7.我的通知列表")
    @ApiOperation(value="我的通知列表", notes="通知查询-所有的通知都返回")
    // 显示表格
    @PostMapping(value = "/showMyNoticeTable")
    public Result<?> showTable(VeOaMeetAnnounceEntity veOaMeetAnnounceEntity){
        HashMap<String, Object> map = veOaPublicService.showCheckMeetAnnounce(veOaMeetAnnounceEntity);
        return Result.OK(map);
    }


    @AutoLog(value = "同步用户数据")
    @ApiOperation(value="同步用户数据", notes="同步用户数据")
    @PostMapping(value = "/synchronizeUser")
    public Result<?> synchronizeUser(String userId){
        xoaCommonUserService.synchronizeUserInfoById(userId);
        return Result.OK();
    }
}
